package com.cskaoyan.connectionpool;

import com.cskaoyan.utils.JDBCUtils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 * 创建日期: 2022/08/26 16:12
 *
 * @author ciggar
 */
public class MyConnectionPoolV1 {

    // 从头部存，从尾部取
    static LinkedList<Connection> pool;

    static {

        // 实例化
        pool = new LinkedList<Connection>();

        // 初始化
        for (int i = 0; i < 10; i++) {

            Connection newConnection = JDBCUtils.getNewConnection();

            pool.addFirst(newConnection);

        }


    }


    // 获取连接
    public static Connection getConnection(){

        // 取出尾部的那个连接
        Connection connection = pool.removeLast();

        return connection;

    }



    // 返回连接
    public static void returnConnection(Connection connection){

        // 把连接放到头部
        pool.addFirst(connection);

    }


}
